package nape.util;






#if flash10
	 import flash.Memory;
	 import flash.utils.ByteArray;
#else
     import nape.Config;
     import nape.Const;
#end



class FastMath 
{
    #if flash10
    	static function __init__() {
			var b = new ByteArray();
			b.length = 1024;
			Memory.select(b);
		}
	#end
	
	
	public static inline function sqrt(x:Float):Float {
		#if flash10
			return 1/invsqrt(x);
		#else
			return Math.sqrt(x);
		#end
	}
	
	
	public static inline function invsqrt(x:Float):Float {
		#if flash10
			Memory.setFloat(0, x);
			Memory.setI32(0, 0x5f3759df - (Memory.getI32(0) >> 1));
			var x2 = Memory.getFloat(0);
			return x2 * (1.5 - 0.5 * x * x2 * x2);
		#else
			return if(x<Const.EPSILON) Const.FMAX else 1.0/sqrt(x);
		#end
	}
}
